package com.sbrc.rediscluster.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @GetMapping(value = "/set")
    public String setKeyValue(){
        redisTemplate.opsForValue().set("ceshi","2222",30, TimeUnit.SECONDS);
        return "成功";
    }

    @GetMapping(value = "/get")
    @Cacheable(value = "cache",key = "'test_' + targetClass.getName()",cacheManager = "redisCacheManager")
    public String getKeyValue(){
        Set<String> sets = redisTemplate.keys("*");
        for (String set : sets) {
            System.out.println(set);
        }
        if(redisTemplate.hasKey("ceshi")){
            return redisTemplate.opsForValue().get("ceshi").toString();
        }
        return "内容为空";
    }


}
